package org.msfj.admin.org.service.impl;

import org.msfj.admin.model.RoleMenu;
import org.msfj.admin.model.User;
import org.msfj.admin.org.service.RoleMenuService;
import org.msfj.admin.vo.RoleMenuVo;
import org.msfj.admin.model.RoleMenu;
import org.msfj.admin.model.User;
import org.msfj.admin.org.service.RoleMenuService;
import org.msfj.admin.vo.RoleMenuVo;
import org.msfj.admin.model.RoleMenu;
import org.msfj.admin.model.User;
import org.msfj.admin.vo.RoleMenuVo;
import org.msfj.admin.org.mapper.RoleMenuMapper;
import org.msfj.admin.org.service.RoleMenuService;
import org.springframework.stereotype.Service;
import org.msfj.common.annotation.Constants;
import org.msfj.common.base.BaseServiceImpl;
import org.msfj.common.enums.Enable;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 系统角色菜单关联信息service实现类
 * @Author: like
 * @Email: like@deyatong.com
 * @Date: 2017-08-10 17:30:20
 * @Version: 1.0
 * @Created in idea by autoCode
 */

@Service
@CacheConfig(cacheNames = Constants.CACHE_NAMESPACE + "roleMenu")
public class RoleMenuServiceImpl extends BaseServiceImpl<RoleMenu> implements RoleMenuService {

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    /**
     * 根据条件分页查询系统角色菜单关联信息列表
     *
     * @param roleMenuVo
     * @param page 当前第几页
     * @param rows 每页多少条记录
     * @return PageInfo<RoleMenu>
     */
    @Override
    //@Cacheable
    public PageInfo<RoleMenuVo> getByRoleMenu(RoleMenuVo roleMenuVo, int page, int rows){
        Example example = new Example(RoleMenu.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("enable", Enable.ENABLE.getCode());
        PageHelper.startPage(page, rows);
        List<RoleMenu> roleMenuList = super.getByExample(example);
        PageInfo roleMenuVoPageInfo = new PageInfo(roleMenuList);
        roleMenuVoPageInfo.setList(setOtherProperties(roleMenuList));
        return roleMenuVoPageInfo;
    }

    /**
     * 根据条件查询所有系统角色菜单关联信息对象列表
     *
     * @param roleMenuVo
     * @return List<RoleMenu>
     */
    @Override
    //@Cacheable
    public List<RoleMenuVo> getAllRoleMenu(RoleMenuVo roleMenuVo){
        Example example = new Example(RoleMenu.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("enable", Enable.ENABLE.getCode());
        if (ObjectUtil.isNotNull(roleMenuVo.getRoleId())) {
            criteria.andEqualTo("roleId", roleMenuVo.getRoleId());
        }
        List<RoleMenuVo> roleMenuVoList = setOtherProperties(super.getByExample(example));
        return roleMenuVoList;
    }

    /**
     * 设置vo的其他属性属性
     *
     * @param roleMenu
     * @return DepartmentVo
     */
    @Override
    //@Cacheable
    public RoleMenuVo setOtherProperties(RoleMenu roleMenu){
        RoleMenuVo roleMenuVo = new RoleMenuVo();
        BeanUtils.copyProperties(roleMenu,roleMenuVo);
        return roleMenuVo;
    }

    /**
     * 设置vo的其他属性属性
     *
     * @param roleMenus
     * @return List<RoleMenuVo>
    */
    @Override
    //@Cacheable
    public List<RoleMenuVo> setOtherProperties(List<RoleMenu> roleMenus){
        List<RoleMenuVo> roleMenuVos = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(roleMenus)){
            for(RoleMenu roleMenu : roleMenus){
                RoleMenuVo roleMenuVo = new RoleMenuVo();
                BeanUtils.copyProperties(roleMenu,roleMenuVo);
                roleMenuVos.add(roleMenuVo);
            }
        }
        return roleMenuVos;
    }

    /**
     * 批量插入角色关联权限
     *
     * @param roleId
     * @param menuIds
     */
    @Override
    @Transactional
    public void saveListByRoleMenu(String roleId, List<String> menuIds, String userId) {
        List<RoleMenu> roleMenus = CollectionUtil.newArrayList();
        Example example = new Example(RoleMenu.class);
        example.createCriteria().andEqualTo("roleId", roleId);
        roleMenuMapper.deleteByExample(example);
        if (CollectionUtil.isNotEmpty(menuIds)) {
            for (String menuId : menuIds) {
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setRoleId(roleId);
                roleMenu.setMenuId(menuId);
                roleMenu.setCreateBy(userId);
                roleMenu.setUpdateBy(userId);
                roleMenus.add(roleMenu);
            }
            super.saveList(roleMenus,User.class);
        }
    }
}
